package cc.z8g.browser.html;

import android.content.res.AssetManager;

import androidx.annotation.NonNull;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import cc.z8g.browser.BrowserApp;
import cc.z8g.browser.core.BrowserConst;

/**
 * AbsListPageFactory
 *
 * @author zhaoxuyang.net
 * @since 2024-08-05
 */
public abstract class AbsListPageFactory implements HtmlPageFactory {
    public static final String HISTORY_FILE_NAME = "history.html";
    public static final String WEB_RES_FILE_NAME = "web_res.html";
    public static final String BOOKMARKS_FILE_NAME = "bookmarks.html";

    /** [下载与历史列表]模板用 */
    public static final String VAR_TITLE = "{{title}}";
    /** [下载与历史列表]模板用 */
    public static final String VAR_BODY_BG_COLOR = "{{bodyBgColor}}";
    /** [下载与历史列表]模板用 */
    public static final String VAR_DIVIDER_COLOR = "{{dividerColor}}";
    /** [下载与历史列表]模板用 */
    public static final String VAR_TITLE_COLOR = "{{titleColor}}";
    /** [下载与历史列表]模板用 */
    public static final String VAR_SUB_TITLE_COLOR = "{{subtitleColor}}";

    /** [书签]模板用 */
    public static final String VAR_BOX_BG_COLOR = "{{boxBgColor}}";
    /** [书签]模板用 */
    public static final String VAR_BOX_TXT_COLOR = "{{boxTxtColor}}";

    /** 开始循环的标记 */
    public static final String VAR_ID_REAPEATED = "id=repeated";
    /** [下载与历史列表][书签]模板用 */
    public static final String VAR_ITEM_HREF = "{{itemHref}}";
    /** [下载与历史列表]模板用 */
    public static final String VAR_ITEM_TEXT = "{{itemText}}";
    /** [下载与历史列表]模板用 */
    public static final String VAR_ITEM_TITLE = "{{itemTitle}}";
    /** [书签]模板用 - 图片地址/图片数据 */
    public static final String VAR_ITEM_IMAGE_SRC= "{{itemImgSrc}}";

    /** 提供 asserts 目录下放置的模块文件路径 */
    abstract String getTemplateFilePath();

    /** 最终保存到哪个文件上 */
    abstract File createPageFile();

    abstract String assignTitle();

    abstract String assignBoxBgColor();
    abstract String assignBoxTxtColor();
    abstract String assignBodyBgColor();
    abstract String assignDividerColor();
    abstract String assignTitleColor();
    abstract String assignSubTitleColor();

    abstract List<? extends Item> assignList();

    public static class Item {
        final String href;
        final String text;
        String title;
        final String imgSrc;

        public Item(String href, String text, String title, String imgSrc) {
            this.href = href;
            this.text = text;
            this.title = title;
            this.imgSrc = imgSrc;
        }
    }

    @NonNull
    @Override
    public String createPage() {
        File dstFile = createPageFile();
        List<? extends Item> list = assignList();
        return onCreateOnePage(dstFile, list);
    }

    /** 书签页需要根据模板创建多个页面，因此抽出此方法进行多次调用 */
    @NonNull
    public String onCreateOnePage(@NonNull File dstFile, @NonNull List<? extends Item> list) {
        AssetManager assets = BrowserApp.getApplication().getAssets();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(assets.open(getTemplateFilePath())));
             BufferedWriter writer = new BufferedWriter(new FileWriter(dstFile));) {
            boolean beginTitle = true;
            boolean beginStyle = true;

            String line;
            while ((line = reader.readLine()) != null) {
                if (beginTitle && line.contains(VAR_TITLE)) {
                    beginTitle = false;
                    String newLine = line.replace(VAR_TITLE, assignTitle());
                    writer.write(newLine);
                    writer.write("\n");
                } else if (beginStyle) {
                    if (line.contains("</style>")) {
                        beginStyle = false;
                        writer.write(line);
                    } else {
                        String newLine = line.replace(VAR_BODY_BG_COLOR, assignBodyBgColor())
                                .replace(VAR_DIVIDER_COLOR, assignDividerColor())
                                .replace(VAR_TITLE_COLOR, assignTitleColor())
                                .replace(VAR_SUB_TITLE_COLOR, assignSubTitleColor())
                                .replace(VAR_BOX_BG_COLOR, assignBoxBgColor())
                                .replace(VAR_BOX_TXT_COLOR, assignBoxTxtColor());
                        writer.write(newLine);
                        writer.write("\n");
                    }
                } else if (line.contains(VAR_ID_REAPEATED)) {
                    for (Item item : list) {
                        String newLine = line.replace(VAR_ITEM_HREF, item.href)
                                .replace(VAR_ITEM_TITLE, item.title)
                                .replace(VAR_ITEM_TEXT, item.text)
                                .replace(VAR_ITEM_IMAGE_SRC, item.imgSrc);
                        writer.write(newLine);
                        writer.write("\n");
                    }
                } else {
                    writer.write(line);
                    writer.write("\n");
                }
            }
            writer.flush();
            return BrowserConst.FILE + dstFile.getAbsolutePath();
        } catch (IOException ex) {
            return BrowserConst.FILE;
        }
    }
}
